<h2 class="hide">用户列表</h2>
<div id="master-user-list"
     class="base-list"
     data-topple="hsList"
     data-load-url="centra/master/user/list.act?unit_id=${unit_id}&amp;ob=mtime!,ctime!&amp;ab=_text&amp;with-signs=1&amp;with-units=2&amp;bind-scope=1"
     data-send-urls-0="['centra/master/user/save.act'  ,'.remove','您确定要移出此用户?']"
     data-send-urls-1="['centra/master/user/delete.act','.delete','您确定要删除此用户?']"
     data-open-urls-0="['centra/master/user/form.html?unit_id=${unit_id}','.create','@']"
     data-open-urls-1="['centra/master/user/form.html?id={ID}','.modify','@']"
     data--1="load:(hsUserLoad)"
     data--2="send:(hsUserSend)">
    <form class="findbox toolbox board row" action="" method="POST">
        <div class="col-xs-7">
            <div class="btn-group">
                <button type="button" class="create btn btn-default">创建用户</button>
                <button type="button" class="modify for-choose btn btn-default">修改</button>
                <button type="button" class="authen for-choose btn btn-default">认证</button>
                <button type="button" class="remove for-choose btn btn-default" title="移出"><span class="bi bi-hi-delete text-danger"></span></button>
                <button type="button" class="delete for-checks btn btn-default" title="删除"><span class="bi bi-hi-remove text-danger"></span></button>
            </div>
        </div>
        <div class="col-xs-5">
            <div class="input-group">
                <input type="search" name="wd" class="form-control input-search"/>
                <span class="input-group-btn">
                    <button type="submit" class="btn btn-default"><span class="bi bi-hi-search"></span></button>
                </span>
            </div>
        </div>
    </form>
    <div class="listbox panel panel-default table-responsive">
        <table class="table table-hover table-striped table-compressed">
            <thead>
                <tr>
                    <th data-fn="id[]" data-ft="_check" class="_check">
                        <input type="checkbox" class="checkall" name="id[]" />
                    </th>
                    <th data-fn="_" data-ft="_admin" class="_admin _amenu">
                        <div class="dropdown invisible">
                            <a href="javascript:;" data-toggle="dropdown"><span class="bi bi-hi-action"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="javascript:;" class="modify">修改</a></li>
                                <li><a href="javascript:;" class="authen">认证</a></li>
                                <li><a href="javascript:;" class="remove"><span class="text-danger">移出</span></a></li>
                                <li><a href="javascript:;" class="delete"><span class="text-danger">删除</span></a></li>
                            </ul>
                        </div>
                    </th>
                    <th data-fn="head" class="userhead">头像</th>
                    <th data-fn="name" class="sortable">名称</th>
                    <th data-fn="username" class="sortable">账号</th>
                    <th data-fn="units..name" data-ft="tips">分组</th>
                    <th data-fn="signs..unit" data-ft="tips">关联</th>
                    <th data-fn="state_text">状态</th>
                    <th data-fn="mtime" data-ft="_htime" data-fill="v*1000" class="_htime sortable">修改时间</th>
                    <th data-fn="ctime" data-ft="_htime" data-fill="v*1000" class="_htime sortable">创建时间</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    <div class="pagebox clearfix"><em class="page-text">...</em></div>

    <div class="modal fade in" style="z-index: 99999; display: none;">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">头像</h4>
                </div>
                <div class="modal-body">
                    <div style="text-align:center;"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    (function($) {
        var context = H$("#master-user-list");
        var viewImg = context.find(".modal");

        hsRequires("centra/master/util.css");

        // 鼠标悬浮在格子上提示
        context.data("_fill_tips" , function(c, v, n) {
            if (v) {
                v = v.sort().join(", ");
                    c.attr("title" , v);
            }
            return  v;
        });

        // 点击用户头像打开预览
        viewImg.modal("hide");
        context.data("_fill_head" , function(c, v, n) {
            if (!v) {
                 v = "static/assets/icons/head_icon.jpg";
                $('<a href="javascript:;" data-href="'+v+'">'
                 +'<span class="img" style="background-image:url('+v+');"/></a>')
                .appendTo(c);
            } else {
                $('<a href="javascript:;" data-href="'+v+'">'
                 +'<span class="img" style="background-image:url('+v+');"/></a>')
                .appendTo(c)
                .click( function( ) {
                    var v = $(this).attr("data-href");
                    viewImg.find(".modal-body div").empty()
                           .append('<img src="'+v+'" style="max-width:100%;margin:0px auto;"/>');
                    viewImg.modal("show");
                });
            }

            // 用户所属的全部部门, 用于拖拽挂职到其他部门
            var unitIds = [];
            if (this._info.units)
            for(var i= 0; i< this._info.units.length;i++) {
                unitIds.push(this._info.units[i].unit_id);
            }
            c.closest( "tr" ).data( "unit_ids" , unitIds);
        });

        // 设置 Auth Code
        context.on("click", ".authen", function() {
            var listobj = context.data ("HsList");
            var ch = listobj.getIds(this);
            if (ch) {
                newAuthCode (ch.val());
            }
        });
        function newAuthCode(id) {
            $.hsMask({
                title: "获取/重置 API Key",
                text : "这将创建新的 API Key, 旧的将会失效(如果有). 确定要执行吗?",
                glass: "alert-danger",
                mode : "warn",
                focus: 2
            }, {
                label: "确定创建",
                glass: "btn-danger",
                click: function() {
                    $.hsAjax({
                        url : "centra/master/user/auth.act",
                        data: { id: id },
                        dataType: "json",
                        success : function(rsp) {
                            rsp = hsResponse(rsp);
                            console.log(rsp.code);
                            if (rsp.ok) {
                                $.hsMask({
                                    html : "请记住并避免泄露, 此后将无法再查看.</p>",
                                    glass: "alert-success",
                                    mode : "warn"
                                }, {
                                    label: "确定并拷贝 API Key",
                                    glass: "btn-success",
                                    click: function() {
                                        $(this).closest(".modal").find(".code").hsCopy();
                                    }
                                }).find(".alert-title").html('API Key: <b class="code">'+ rsp.code +'</b>');
                            }
                        }
                    });
                }
            }, {
                label: "取消",
                glass: "btn-default"
            });
        }
    })(jQuery);
</script>